<template>

  <div class="score-case">
      <!-- 顶部搜索框模块 -->
        <div class="input-group">
          <h2>品牌管理</h2>
        </div>
       
      <div class="table">
      <table >
        <thead>
          <tr>
            <th>编号</th>
            <th>资产名称</th>
            <th>价格</th>
            <th>创建时间</th>
            <th>操作</th>
          </tr>
        </thead>
        <tbody v-if='list.length > 0'>
          <tr v-for='item in list' :key='item.id'>
            <td>{{item.id}}</td>
            <td>{{item.name}}</td>
            <td :class="item.price >100 ? 'red': ''">{{item.price}}</td>
            <td>{{ dateFormat(item.time)}}</td>
            <td><a href="#" @click.prevent='del(item.id)' >删除</a></td>
          </tr>
        </tbody>
        <tbody v-else>
          <tr>
            <td colspan="5">
              <span class="none">暂无数据</span>
            </td>
          </tr>
        </tbody>
        <tfoot>
          <tr>
            <td>统计：</td>
            <td colspan="2">
              <span >总价钱为：{{total}}</span>
              <!-- <span style="margin-left: 50px">平均分：80.25</span> -->
            </td>
             <td colspan="2">
              <span>平均价：{{list.length ? avg : 0}}</span>
            </td>
          </tr>
        </tfoot>
      </table>
    </div>
    <div class="form">
      <div class="form-item">
        <!-- <div class="label">科目：</div> -->
        <div class="input">
          <input type="text" v-model="name" placeholder="资产名称" />
        </div>
      </div>
      <div class="form-item">
        <!-- <div class="label">分数：</div> -->
        <div class="input">
          <input type="text" v-model="price" placeholder="0" />
        </div>
      </div>
      <div class="form-item">
        <div class="label"></div>
        <div class="input"  >
          <button class="submit" @click.prevent='add'>添加资产</button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import dayjs from 'dayjs'
export default {
  name: 'ScoreCase',
  data() {
    return {
       list: JSON.parse(localStorage.getItem('score-case')) || [ ],
         //  { id: 100, name: "外套", price: 199, time: new Date('2010-08-12') },
         //  { id: 101, name: "裤子", price: 34, time: new Date('2013-09-01') },
         //  { id: 102, name: "鞋", price: 25.4, time: new Date('2018-11-22') },
         //  { id: 103, name: "头发", price: 19900, time: new Date('2020-12-12') }
     
        name: "", // 名称
        price: 0, // 价格
    }
  },
  methods:{
     del(id){
      this.list = this.list.filter(item =>item.id != id)
      console.log(id);
     },  
  dateFormat(d) {
    return dayjs(d).format('YYYY-MM-DD ')
   },
   add(){
       if(this.name === '' || this.price === 0 ){
         return alert('请输入正确的名字和价格')        
       }
      this.list.push({
       id:+new Date(), name:this.name, price:this.price, time: new Date() 
      })
     this.name = "", // 名称
     this.price =  0
   }
  },
//   计算属性
  computed:{
    total(){
      return parseInt(this.list.reduce((p,t)=> p+t.price,0)) 
    },
    avg(){
      return parseInt((this.total / this.list.length))
    }
  },
//   侦听器
watch:{
   list(val){
   localStorage.setItem('score-case',JSON.stringify(val))
   }
}
}
</script>

<style>
  .input-group{
   text-align: center;
  }
.score-case {
  width: 1000px;
  margin: 50px auto;
  /* display: flex; */
}
.score-case .table {
  flex: 4;
}
.score-case .table table {
  width: 100%;
  border-spacing: 0;
  border-top: 1px solid #ccc;
  border-left: 1px solid #ccc;
}
.score-case .table table th {
  background: #f5f5f5;
}
.score-case .table table tr:hover td {
  background: #f5f5f5;
}
.score-case .table table td,
.score-case .table table th {
  border-bottom: 1px solid #ccc;
  border-right: 1px solid #ccc;
  text-align: center;
  padding: 10px;
}
.score-case .table table .red {
  color: red;
}
.score-case .table table td a{
 text-decoration: none;
}
.score-case .table .none {
  height: 50px;
  line-height: 50px;
  color: #999;
}
.score-case .form {
  flex: 1;
  padding: 20px;
  display: flex;
}
.score-case .form .form-item {
  display: flex;
  margin-bottom: 20px;
  align-items: center;
}
.score-case .form .form-item .label {
  width: 60px;
  text-align: right;
  font-size: 14px;
}
.score-case .form .form-item .input {
  flex: 1;
}
.score-case .form .form-item input,
.score-case .form .form-item select {
  appearance: none;
  outline: none;
  border: 1px solid #ccc;
  width: 200px;
  height: 40px;
  box-sizing: border-box;
  padding: 10px;
  color: #666;
}
.score-case .form .form-item input::placeholder {
  color: #666;
}
.score-case .form .form-item .cancel,
.score-case .form .form-item .submit {
  appearance: none;
  outline: none;
  border: 1px solid #ccc;
  border-radius: 4px;
  padding: 4px 10px;
  margin-right: 10px;
  font-size: 12px;
  background: #ccc;
}
.score-case .form .form-item .submit {
  border-color: #069;
  background: #069;
  color: #fff;
}
</style>